﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ComunesMGI.DataTypes;
using MGI.Logica;
using System.IO;

namespace WebServiceMGI
{
    public class MGIIncidentesWS : IMGIIncidentesWS
    {
        public List<DataCampo> getCampos(DataCliente cliente, DataSistema sistema)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes incidentes = logicaFactory.getIIncidentes();
            return incidentes.getCampos(cliente, sistema);
        }

        public DataAltaIncidente addIncidente(DataSistema incidente, List<DataAttachment> archivos)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes incidentes = logicaFactory.getIIncidentes();
            return incidentes.addIncidente(incidente, archivos);
        }

        public List<DataIncidente> getIncidentes(DataCliente cliente, DataSistema sistema)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes incidentes = logicaFactory.getIIncidentes();
            return incidentes.getIncidentes(cliente, sistema);
        }

        public List<DataTarea> getTareas(DataCliente cliente, DataSistema sistema, DataIncidente incidente)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes incidentes = logicaFactory.getIIncidentes();
            return incidentes.getTareas(cliente, sistema, incidente);
        }

        public bool asignarResponsable(DataSistema sistema, DataIncidente incidente, DataTarea tarea, DataUsuario usuario)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes incidentes = logicaFactory.getIIncidentes();
            return incidentes.asignarResponsable(sistema, incidente, tarea, usuario);

        }

        public bool addWorkPackage(DataCliente cliente, DataSistema sistema, DataWP workPackage)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes incidentes = logicaFactory.getIIncidentes();
            return incidentes.addWorkPackage(cliente, sistema, workPackage);
        }

        public List<DataWP> getWorkPackages(DataCliente cliente, DataSistema sistema)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes incidentes = logicaFactory.getIIncidentes();
            return incidentes.getWorkPackages(cliente, sistema);
        }

        public void asociarIncidenteWP(DataCliente cliente, DataSistema sistema, DataWP workPackage, List<DataIncidente> incidentes)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes inc = logicaFactory.getIIncidentes();
            inc.asociarIncidenteWP(cliente, sistema, workPackage, incidentes);
        }

        public List<DataIncidenteReporte> getIncidentesCerradosPeriodo(List<DataSistema> sistemas, DateTime perFchIni, DateTime perFchFin)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes inc = logicaFactory.getIIncidentes();
            return inc.getIncidentesCerradosPeriodo(sistemas, perFchIni, perFchFin);
        }

        public List<DataIncidente> getIncidentesWP(DataSistema sistema, DataWP workPackage)
        {
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes inc = logicaFactory.getIIncidentes();
            return inc.getIncidentesWP(sistema, workPackage);
        }

        public string UploadFile(Stream sourceStream)
        {
            FileStream targetStream = null;

            string uploadFolder = @".\";
            int nroTemp = 0;
            
            DirectoryInfo dir = new DirectoryInfo(uploadFolder);
            bool existe = true;
            while (existe)
            {
                existe = false;
                foreach (FileInfo fInfo in dir.GetFiles())
                {
                    if (fInfo.Name == "Upload" + nroTemp.ToString() + ".tmp")
                    {
                        nroTemp++;
                        existe = true;
                    }

                }
            }
            string filename = "Upload" + nroTemp.ToString()+ ".tmp";
            string filePath = Path.Combine(uploadFolder, filename);

            FileInfo archivo = new FileInfo(filePath);
            try
            {
                using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    const int bufferLen = 4096;
                    byte[] buffer = new byte[bufferLen];
                    int count = 0;
                    while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
                    {
                        targetStream.Write(buffer, 0, count);
                    }
                    targetStream.Close();
                    sourceStream.Close();
                }
                return nroTemp.ToString();
            }
            catch (Exception e)
            {
                throw new Exception("No se pudo crear el archivo temporal" + e.Message);
            }
        }

        public bool adjuntarArchivoIncidente(string nombreArchivo, DataSistema sistema, DataIncidente incidente, string nroTemp) 
        { 
            LogicaFactory logicaFactory = LogicaFactory.getInstance();
            IIncidentes incidentes = logicaFactory.getIIncidentes();
            return incidentes.adjuntarArchivoIncidente(nombreArchivo , sistema, incidente, nroTemp);
        }


    }
}